import type { CreateCrudOptionsRet, ScopeContext } from '@fast-crud/fast-crud';

import type { FormRulesExt } from '#/api';
import type { ExtendMsgModel } from '#/api/basic/msg/model/extendMsgModel';
import type { ActionEnum } from '#/enums/commonEnum';

import { h } from 'vue';

import { isEmpty } from 'lodash-es';

import { RuleType } from '#/api';
import { ExtendMsgApi } from '#/api/basic/msg/extendMsg';

import ParamList from '../modules/param-list.vue';
import RecipientList from '../modules/recipient-list.vue';

export function createCrudOptions(): CreateCrudOptionsRet<ExtendMsgModel.MsgSendVO> {
  return {
    crudOptions: {
      request: { addRequest: ExtendMsgApi.sendByTemplateRequest },
      form: { mode: 'add' },
      columns: {
        code: {
          title: '标识',
          type: 'text',
          form: { component: { readonly: true } },
        },
        title: {
          title: '标题',
          type: 'text',
          form: { component: { readonly: true } },
        },
        name: {
          title: '模版名称',
          type: 'text',
          form: { component: { readonly: true } },
        },
        bizId: {
          title: '业务id',
          type: 'text',
        },
        bizType: {
          title: '业务类型',
          type: 'text',
        },
        author: {
          title: '发布人姓名',
          type: 'text',
        },
        paramList: {
          title: '参数',
          type: 'text',
          form: {
            col: { span: 24 },
            render: (scope: ScopeContext) => {
              return h(ParamList, {
                value: scope.form.param,
                'onUpdate:value': (newValue: any) => {
                  scope.form.paramList = newValue;
                },
              });
            },
          },
        },
        recipientList: {
          title: '接收人',
          type: 'text',
          form: {
            col: { span: 24 },
            render: (scope: ScopeContext) => {
              return h(RecipientList, {
                value: scope.form?.recipientList,
                'onUpdate:value': (newValue: any) => {
                  scope.form.recipientList = newValue;
                },
              });
            },
          },
        },
      },
    },
  };
}

export const frontRules = (_mode: ActionEnum): FormRulesExt => {
  return {
    paramList: {
      type: RuleType.and,
      rules: [
        {
          trigger: 'blur',
          type: 'array',
          asyncValidator: async (_: any, values: any) => {
            if (values) {
              for (const i in values) {
                if (isEmpty(values[i].key)) {
                  throw new Error(`第${Number(i) + 1}行未填写参数名称`);
                }
                if (isEmpty(values[i].value)) {
                  throw new Error(`第${Number(i) + 1}行未填写参数值`);
                }
              }
            }
          },
        },
      ],
    },
    recipientList: {
      type: RuleType.cover,
      rules: [
        {
          trigger: 'blur',
          type: 'array',
          asyncValidator: async (_: any, values: any) => {
            if (values) {
              for (const i in values) {
                if (isEmpty(values[i].recipient)) {
                  throw new Error(`第${Number(i) + 1}行未填写接收人`);
                }
                if (isEmpty(values[i].ext)) {
                  throw new Error(`第${Number(i) + 1}行未填写扩展信息`);
                }
              }
            } else {
              throw new Error(`请至少填写1个接收人`);
            }
          },
        },
      ],
    },
  };
};
